package com.zhijiaoyun.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zhijiaoyun.common.CommonUtils;
import com.zhijiaoyun.common.Constant;
import com.zhijiaoyun.common.DsConstant;
import com.zhijiaoyun.common.ResponseUtils;
import com.zhijiaoyun.common.StringUtils;
import com.zhijiaoyun.dao.IAdminDao;
import com.zhijiaoyun.dao.IAuthorityDao;
import com.zhijiaoyun.model.Admin;
import com.zhijiaoyun.model.Authority;
import com.zhijiaoyun.model.dto.PageData;
import com.zhijiaoyun.model.dto.PageList;
import com.zhijiaoyun.service.IAdminService;

/**
 * @author shisfish
 * @date 2017/6/6
 */
@Service(value = "adminService")
public class AdminServiceImpl implements IAdminService {

	@Resource
	IAdminDao adminDao;
	
	@Resource
	IAuthorityDao authorityDao;
	
	@Override
	public Admin getAdminByAccount(String account) {
		return adminDao.selectAdminByAccount(account);
	}

	@Override
	public PageList<Admin> pageByParams(int pageIndex, int pageSize, String account, String username, int status) {
		int count = adminDao.countByParams(account, username, status);
		PageList<Admin> pageList = new PageList<>();
		if (count > 0) {
			PageData pageData = PageData.createPage(count, pageIndex, pageSize);
			List<Admin> list = adminDao.pageByParams(pageData.getBeginIndex(), pageData.getPageSize(), account, username, status);
			if (StringUtils.isNotEmptyList(list)) {
				for (Admin admin : list) {
					String authorityName = authorityDao.selectNameById(admin.getAuthorityId());
					admin.setAuthorityName(authorityName);
				}
			}
			pageList = new PageList<>(list, pageData);
		}
		return pageList;
	}

	@Override
	public boolean isAllowed(long adminId, long authorityId) {
		Authority authority = authorityDao.selectByAdminId(adminId);
		if (authority != null && !StringUtils.isEmptyString(authority.getModuleIds())) {
			String[] authorities = authority.getModuleIds().split(",");
			for (String string : authorities) {
				if (authorityId == StringUtils.parseLong(string, 0)) {
					return true;
				}
			}
		}
		return false;
	}

	@Override
	public Map<String, Object> addAdmin(String account, String username, String password, Long authorityId, Integer status, String createUser) {
		Admin admin = getAdminByAccount(account);
		if (admin != null) {
			return ResponseUtils.responseErrorMap(DsConstant.CODE_ADMIN_ACCOUNT_EXIST, DsConstant.MESSAGE_ADMIN_ACCOUNT_EXIST);
		}
		admin = new Admin(account, username, CommonUtils.MD5(password), status, authorityId, createUser);
		insert(admin);
		return ResponseUtils.responseSuccessMap();
	}

	@Override
	public Map<String, Object> getAdminById(long adminId) {
		Admin admin = getById(adminId);
		Map<String, Object> map = new HashMap<>();
		map.put("obj", admin);
		return ResponseUtils.responseSuccessMap(map);
	}

	@Override
	public Map<String, Object> page(int pageIndex, int pageSize, String account, String username, Integer status) {
		PageList<Admin> adminPage = pageByParams(pageIndex, pageSize, account, username, status == null ? -1 : status);
		Map<String, Object> map = new HashMap<>();
		map.put("page", adminPage.getPageData());
		map.put("list", adminPage.getList());
		return ResponseUtils.responseSuccessMap(map);
	}

	@Override
	public void insert(Admin admin) {
		adminDao.insert(admin);
	}

	@Override
	public void update(Admin admin) {
		adminDao.updateByPrimaryKey(admin);
	}

	@Override
	public void delete(Admin admin) {
		adminDao.deleteByPrimaryKey(admin.getAdminId());
	}

	@Override
	public void deleteById(long id) {
		Admin admin = adminDao.selectByPrimaryKey(id);
		if (admin != null && !Constant.ADMIN.equals(admin.getAccount())) {
			delete(admin);
		}
	}

	@Override
	public Admin getById(long id) {
		return adminDao.selectByPrimaryKey(id);
	}

	@Override
	public PageList<Admin> page(int pageIndex, int pageSize, Object... args) {
		return null;
	}
}
